/**
 * 
 */
package com.nari.baseinfo.service;

import java.io.File;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import com.nari.baseinfo.dao.ICityDao;
import com.nari.baseinfo.dao.IFileMappingDao;
import com.nari.baseinfo.dao.IHeatStationDao;
import com.nari.baseinfo.model.IFileMapping;
import com.nari.baseinfo.model.IHeatStation;
import com.nari.coreframe.service.GenericServiceImpl;
import com.nari.platform.date.CalendarUtils;
import com.nari.platform.model.PageCond;
import com.nari.platform.model.ResponseMsg;
import com.nari.platform.model.ResultWithPageCond;

/**
 * 
 * @author Xiaopeng
 * @date 2013-8-1
 */
public class HeatStationServiceImpl extends GenericServiceImpl<IHeatStation, String> implements
		IHeatStationService {
	private String diskFilePath;
	private String stationPicAttachFolder;
	private ICityDao iCityDao;
	private IFileMappingDao iFileMappingDao;

	/**
	 * 根据所属城市ID查询换热站信息
	 * @param districtCityID 所属区级机构(可选)
	 * @param capitalCityID 所属市级机构(可选)
	 * @param provinceCityID 所属省级机构(可选)
	 * @return
	 * @throws Exception
	 * @author Xiaopeng
	 * @date 2013-8-23
	 */
	public List<IHeatStation> queryStationByBelongCity(String districtCityID, 
			String capitalCityID, String provinceCityID) throws Exception {
		IHeatStationDao iHeatStationDao = (IHeatStationDao) this.getiGenericDao();
		return iHeatStationDao.queryStationByBelongCity(districtCityID, capitalCityID, provinceCityID);
	}
	
	/**
	 * 分页查询换热站信息
	 * @param stationName 换热站名称(可选)
	 * @param districtCityID 所属区级机构(可选)
	 * @param capitalCityID 所属市级机构(可选)
	 * @param provinceCityID 所属省级机构(可选)
	 * @param pageCond
	 * @return
	 * @throws Exception
	 * @author Xiaopeng
	 * @date 2013-8-7
	 */
	public ResultWithPageCond<IHeatStation> queryStationWithPageCond(String stationName, 
			String districtCityID, String capitalCityID, String provinceCityID, PageCond pageCond) throws Exception {
		IHeatStationDao iHeatStationDao = (IHeatStationDao) this.getiGenericDao();
		return iHeatStationDao.queryStationWithPageCond(stationName, districtCityID, capitalCityID, provinceCityID, pageCond);
	}
	
	/**
	 * 通过换热站列表查询与之关联的行政机构ID-机构名称映射表
	 * @param stationList 换热站列表，若列表为空，则返回空的映射表
	 * @return
	 * @throws Exception
	 * @author Xiaopeng
	 * @date 2013-8-7
	 */
	public Map<String, String> queryReferCitysByStationList(List<IHeatStation> stationList) throws Exception {
		List<String> cityIDList = null;
		if(stationList!=null && stationList.size()>0) {
			cityIDList = new ArrayList<String>();
			for(IHeatStation heatStation: stationList) {
				cityIDList.add(heatStation.getProvinceCityID());
				cityIDList.add(heatStation.getCapitalCityID());
				cityIDList.add(heatStation.getDistrictCityID());
			}
		}
		return iCityDao.queryIdNameMapByIDList(cityIDList);
	}
	
	/**
	 * 带附件上传的换热站信息添加及修改
	 * @param heatStation 换热站信息实体
	 * @param attachFile 安装图片附件
	 * @param fileName 附件文件名
	 * @return
	 * @throws Exception
	 * @author Xiaopeng
	 * @date 2013-8-24
	 */
	public ResponseMsg saveOrUpdateHeatStation(IHeatStation heatStation, File attachFile, 
			String fileName) throws Exception {
		String deleteFileMapUID = null; //修改热量表附件时，则删除原附件
		//带附件上传的换热站信息提交
		if(attachFile != null) {
			File svFile = null; //存储磁盘附件
			try {
				//附件存储路径对应的磁盘目录检测
				File fileSaveFolder = new File(this.diskFilePath+"/"+this.stationPicAttachFolder);
				if(!fileSaveFolder.exists()) fileSaveFolder.mkdirs();
				//文件名称
				String orFilename = fileName.substring(0, fileName.lastIndexOf("."));
				//文件类型
				String fileType = fileName.substring(fileName.lastIndexOf(".")+1);
				//电子文件实体映射
				IFileMapping fileMapping = new IFileMapping();
				fileMapping.setOrFilename(orFilename);
				fileMapping.setDiskID(diskFilePath);
				fileMapping.setFileType(fileType);
				fileMapping.setFileSize(attachFile.length());
				fileMapping.setCreateTime(new Date());
				//读取文件映射的唯一标识
				String fileMappingUID = this.iFileMappingDao.saveEntity(fileMapping);
				//文件存储的随机文件名
				String svFilename = fileMappingUID + "-" + CalendarUtils.parseDateToString(new Date(), "yyyy-MM-dd-HH-mm-ss");
				fileMapping.setSvFilename(svFilename);
				//文件的存储路径(相对路径)
				String savePath = this.stationPicAttachFolder + "/" + svFilename + "." + fileType;
				fileMapping.setSavePath(savePath);
				this.iFileMappingDao.updateEntity(fileMapping, fileMappingUID);
				//设置换热站附件引用信息
				deleteFileMapUID = heatStation.getInstallPic();
				heatStation.setInstallPic(fileMappingUID); 
				//将附件文件保存到磁盘中
				svFile = new File(this.diskFilePath + "/" + savePath);
				this.iFileMappingDao.saveUpload(attachFile, svFile);
			} catch (Exception e) {
				e.printStackTrace();
				if(svFile.exists()) svFile.delete();
				throw new Exception(e.getMessage());
			}
		}
		//保存换热站信息
		if(heatStation.getId()!=null && !"".equals(heatStation.getId())) {
			this.getiGenericDao().updateEntity(heatStation, heatStation.getId());
			//删除换热站原附件
			if(deleteFileMapUID!=null && !"".equals(deleteFileMapUID)) {
				this.iFileMappingDao.deleteFileMapByUID(deleteFileMapUID);
			}
		} else {
			this.getiGenericDao().saveEntity(heatStation);
		}
		return ResponseMsg.getInstance(true, "");
	}
	
	/**
	 * 删除换热站附件
	 * @param heatStationUID
	 * @return
	 * @throws Exception
	 * @author Xiaopeng
	 * @date 2013-8-11
	 */
	public ResponseMsg deleteStationPicAttachFile(String heatStationUID) throws Exception {
		IHeatStation heatStation = this.getiGenericDao().queryEntityById(heatStationUID);
		//删除换热站附件
		this.iFileMappingDao.deleteFileMapByUID(heatStation.getInstallPic());
		//更新换热站信息
		heatStation.setInstallPic(null);
		this.getiGenericDao().updateEntity(heatStation, heatStationUID);
		return ResponseMsg.getInstance(true, "");
	}
	
	/**
	 * 通过换热站唯一标识删除换热站信息，并批次删除对应的换热站附件等
	 * @param heatStationUID
	 * @return
	 * @throws Exception
	 * @author Xiaopeng
	 * @date 2013-8-12
	 */
	public ResponseMsg deleteHeatStationByUID(String heatStationUID) throws Exception {
		IHeatStation heatStation = this.getiGenericDao().queryEntityById(heatStationUID);
		//删除换热站附件
		if(heatStation.getInstallPic()!=null && !"".equals(heatStation.getInstallPic())) {
			this.iFileMappingDao.deleteFileMapByUID(heatStation.getInstallPic());
		}
		//删除换热站信息
		this.getiGenericDao().deleteEntityById(heatStationUID);
		return ResponseMsg.getInstance(true, "");
	}

	/**
	 * @return the diskFilePath
	 */
	public String getDiskFilePath() {
		return diskFilePath;
	}

	/**
	 * @param diskFilePath the diskFilePath to set
	 */
	public void setDiskFilePath(String diskFilePath) {
		this.diskFilePath = diskFilePath;
	}

	/**
	 * @return the stationPicAttachFolder
	 */
	public String getStationPicAttachFolder() {
		return stationPicAttachFolder;
	}

	/**
	 * @param stationPicAttachFolder the stationPicAttachFolder to set
	 */
	public void setStationPicAttachFolder(String stationPicAttachFolder) {
		this.stationPicAttachFolder = stationPicAttachFolder;
	}

	/**
	 * @return the iCityDao
	 */
	public ICityDao getiCityDao() {
		return iCityDao;
	}

	/**
	 * @param iCityDao the iCityDao to set
	 */
	public void setiCityDao(ICityDao iCityDao) {
		this.iCityDao = iCityDao;
	}

	/**
	 * @return the iFileMappingDao
	 */
	public IFileMappingDao getiFileMappingDao() {
		return iFileMappingDao;
	}

	/**
	 * @param iFileMappingDao the iFileMappingDao to set
	 */
	public void setiFileMappingDao(IFileMappingDao iFileMappingDao) {
		this.iFileMappingDao = iFileMappingDao;
	}
	
}
